<?php

namespace App\Providers;

use App\Admin\Models\AdminPermission;
use App\Admin\Models\AdminUser;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
        'App\Http\Models\Post' => 'App\Policies\PostPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        // 注册门卫Gate
        $permissions = [];
        if (Schema::hasTable('admin_permissions')) {
            $permissions = AdminPermission::all();
        }
        foreach ($permissions as $permission) {
            Gate::define($permission->name, function ($user) use ($permission) {
                /* @var AdminUser $user */
                return $user->hasPermission($permission);
            });
        }
    }
}
